package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = Setmeal.builder().build();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.add(setmeal);

        Long id = setmeal.getId(); // 獲得套餐的自增主鍵

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });

        setmealDishMapper.addDish(setmealDishes);

    }

    /**
     * 套餐分頁查詢
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量刪除
     * @param ids
     */
    @Override
    public void deletByIds(List<Long> ids) {

        /**
         * 刪除套餐
         * 1.判斷狀態，起售中的套餐不能刪除
         * 2.刪除套餐操作;
         * 2.1~刪除 setmeal 表中的數據
         * 2.2~刪除與 setmeal 關聯的 setmeal_dish 表中的數據
         * setmeal 表中的主鍵與 setmeal_dish 中的setmeal_id 相對應
         *
         */

        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);

            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(id -> {

            setmealMapper.deletById(id); //刪除套餐

            setmealDishMapper.deletBy_id(id); //刪除套餐裏關聯的菜品
        });



    }

    /**
     * 禁用啓用
     * @param status
     * @param id
     */
    @Override
    public void StartOrStop(Integer status, Long id) {
//       Setmeal setmeal = Setmeal.builder().id(id).status(status).updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId()).build();
        Setmeal setmeal = setmealMapper.getById(id);
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        /**
         * 先修改 seatmeal 裏的數據
         * 再修改 seatmeal_dish 裏的數據
         */
        Setmeal setmeal = Setmeal.builder().build();

        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal); // 修該套餐表的數據

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes(); // 獲得套倉裏的菜品

        Long id = setmealDTO.getId();
//        判斷套倉裏面是否有菜品
        if (setmealDishes.size() > 0 && setmealDishes != null) {
            setmealDishMapper.deletBy_id(id);
        }

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });

        setmealDishMapper.addDish(setmealDishes); // 添加菜品
    }

    @Override
    public SetmealVO select(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);

        List<SetmealDish> list = setmealDishMapper.getById(id);

        SetmealVO setmealVO = SetmealVO.builder().build();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
